﻿using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesFile;

namespace CheckData
{
    public class Gis
    {
        #region AE认证
        private static IAoInitialize _aoInitialize;
        /// <summary>
        /// 初始化AE控件
        /// </summary>
        public static bool InitLicense()
        {
#if ARCGIS9

#else
            if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop))
            {
                throw new Exception("没有安装ArcEngine,系统无法运行!");
            }
#endif
            _aoInitialize = new AoInitializeClass();
            if (_aoInitialize.IsProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB) == esriLicenseStatus.esriLicenseAvailable &&
                _aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB) == esriLicenseStatus.esriLicenseCheckedOut)
            {
                return true;
            }
            else
            {
                throw new Exception("缺少GDB许可，请重新安装ArcEngine!");
            }
        }

        public static void ShutDownLicense()
        {
            _aoInitialize.Shutdown();
        }
        #endregion
        private static IFeatureWorkspace _workspace;
        /// <summary>
        /// 工作空间
        /// </summary>
        public static IFeatureWorkspace Workspace
        {
            get { return _workspace; }
            set { _workspace = value; }
        }
        /// <summary>
        /// 获取SDE工作空间
        /// </summary>
        /// <param name="server">服务器</param>
        /// <param name="user">用户</param>
        /// <param name="pass">密码</param>
        /// <param name="version">版本</param>
        /// <param name="port">端口</param>
        /// <returns></returns>
        public static IFeatureWorkspace GetWorkSpaceFromSDE(string server, string user, string pass, string version, string port)
        {
            try
            {
                IPropertySet propSet = new PropertySetClass();
                propSet.SetProperty("server", server);
                propSet.SetProperty("instance", port);
                propSet.SetProperty("user", user);
                propSet.SetProperty("password", pass);
                propSet.SetProperty("version", version);
                SdeWorkspaceFactory factory = new SdeWorkspaceFactoryClass();
                return factory.Open(propSet, 0) as IFeatureWorkspace;
            }
            catch (Exception ex) { throw new Exception("连接SDE失败！"); }
        }
        /// <summary>
        /// 获取SDE工作空间
        /// </summary>
        /// <param name="server">服务器</param>
        /// <param name="user">用户</param>
        /// <param name="pass">密码</param>
        /// <returns></returns>
        public static IFeatureWorkspace GetWorkSpaceFromSDE(string server, string user, string pass)
        {
            return GetWorkSpaceFromSDE(server, user, pass, "SDE.DEFAULT", "5151") as IFeatureWorkspace;
        }
        /// <summary>
        /// 获取MDB文件工作空间
        /// </summary>
        /// <param name="strMdb"></param>
        /// <returns></returns>
        public static IFeatureWorkspace GetWorkSpaceFromMDB(string strMdb)
        {
            try
            {
                IWorkspaceFactory factory = new AccessWorkspaceFactoryClass();
                return factory.OpenFromFile(strMdb, 0) as IFeatureWorkspace;
            }
            catch (Exception ex) { throw ex; }
        }
        /// <summary>
        /// 打开GDB工作空间
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static IFeatureWorkspace GetWorkSpaceFromGDB(string fileName)
        {
            try
            {
                IWorkspaceFactory factory = new FileGDBWorkspaceFactoryClass();
                return factory.OpenFromFile(fileName, 0) as IFeatureWorkspace;
            }
            catch (Exception ex) { throw ex; }
        }
        /// <summary>
        /// 打开SHP工作空间
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static IFeatureWorkspace GetWorkSpaceFromSHP(string fileName)
        {
            try
            {
                string path = System.IO.Path.GetDirectoryName(fileName);
                IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
                return workspaceFactory.OpenFromFile(path, 0) as IFeatureWorkspace;
            }
            catch (Exception ex) { throw ex; }
        }
        /// <summary>
        /// 获取SDE工作空间，通过Oracle直连方式 11G
        /// </summary>
        /// <param name="oraServer"></param>
        /// <param name="user"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static IFeatureWorkspace GetWorkspaceOracle11G(string oraServer, string user, string pwd)
        {
            try
            {
                IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactoryClass();
                IPropertySet propSet = new PropertySetClass();
                propSet.SetProperty("SERVER", "");
                propSet.SetProperty("INSTANCE", "sde:oracle11g");
                propSet.SetProperty("USER", user);
                propSet.SetProperty("PASSWORD", pwd + "@" + oraServer);
                propSet.SetProperty("VERSION", "SDE.DEFAULT");
                return workspaceFactory.Open(propSet, 0) as IFeatureWorkspace;
            }
            catch (Exception ex) { throw ex; }
        }
        /// <summary>
        /// 获取SDE工作空间，通过Oracle直连方式 10G
        /// </summary>
        /// <param name="oraServer"></param>
        /// <param name="user"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static IFeatureWorkspace GetWorkspaceOracle10G(string oraServer, string user, string pwd)
        {
            try
            {
                IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactoryClass();
                IPropertySet propSet = new PropertySetClass();
                propSet.SetProperty("SERVER", "");
                propSet.SetProperty("INSTANCE", "sde:oracle10g");
                propSet.SetProperty("USER", user);
                propSet.SetProperty("PASSWORD", pwd + "@" + oraServer);
                propSet.SetProperty("VERSION", "SDE.DEFAULT");
                return workspaceFactory.Open(propSet, 0) as IFeatureWorkspace;
            }
            catch (Exception ex) { throw ex; }
        }
    }
}
